﻿using LoA.Shared.Interfaces;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Microsoft.Xna.Framework {
	public static class ModelExtension {
		public static void DrawOnPosition(this Model model, Vector3 Position, Vector3 Direction, Vector3 Up, ICamera Camera) {
			Matrix[ ] boneTransforms = new Matrix[model.Bones.Count];
			model.CopyAbsoluteBoneTransformsTo(boneTransforms);

			Matrix worldMatrix = Matrix.CreateWorld(Position, Direction, Up);

			// Strasznie wyblaknięte i bez życia rysowanie
			//model.Draw(worldMatrix, Camera.ViewMatrix, Camera.ProjectionMatrix);

			foreach (ModelMesh mesh in model.Meshes) {
				foreach (BasicEffect effect in mesh.Effects) {
					effect.World = boneTransforms[mesh.ParentBone.Index] * worldMatrix;
					effect.View = Camera.ViewMatrix;
					effect.Projection = Camera.ProjectionMatrix;

					// Set default lighting
					effect.EnableDefaultLighting( );
					effect.PreferPerPixelLighting = true;

					// Set the fog to match the black background color
					effect.FogEnabled = true;
					effect.FogColor = Vector3.Zero;
					effect.FogStart = 1000;
					effect.FogEnd = 3200;
				}
				mesh.Draw( );
			}
		}

		public static void DrawMap(this Model model, ICamera Camera) {
			Matrix worldMatrix = Matrix.Identity;
			if (model.Tag.GetType( ).GetInterface(typeof(IMapInfo).Name) != null) {
				Vector2 position = (model.Tag as IMapInfo).GetRealPosition( );
				worldMatrix = Matrix.CreateTranslation(position.X, 0, position.Y);
			}
			Matrix[ ] boneTransforms = new Matrix[model.Bones.Count];
			model.CopyAbsoluteBoneTransformsTo(boneTransforms);

			// Strasznie wyblaknięte i bez życia rysowanie
			//model.Draw(worldMatrix, Camera.ViewMatrix, Camera.ProjectionMatrix);

			foreach (ModelMesh mesh in model.Meshes) {
				foreach (BasicEffect effect in mesh.Effects) {
					effect.World = boneTransforms[mesh.ParentBone.Index] * worldMatrix;
					effect.View = Camera.ViewMatrix;
					effect.Projection = Camera.ProjectionMatrix;

					// Set default lighting
					effect.EnableDefaultLighting( );
					effect.PreferPerPixelLighting = true;

					// Set the fog to match the black background color
					effect.FogEnabled = true;
					effect.FogColor = Vector3.Zero;
					effect.FogStart = 1000;
					effect.FogEnd = 3200;
				}
				mesh.Draw( );
			}
		}
	}
}